package com.atguigu.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msmservice.service.MsmService;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    @Override
    public boolean sends(Map<String, Object> param, String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5t8bVfdmz9cyDBLZynbt", "KzbcPPZBSWtZpn2hIjC0zIP7jRjnQn");

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.putQueryParameter("PhoneNumbers",phone); //手机号
        String code = (String) param.get("code");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递
//        request.setTemplateParam("{\"code\":\"1234\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            String success = response.getCode();
            if(success.equals("OK")){
                return true;
            }
            System.out.println(new Gson().toJson(response));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
